﻿using System;
using Microsoft.IdentityModel.Web;
using CodeCamp.Infrastructure;

namespace CodeCamp
{
    public static class FederatedAuthConfig
    {
        public enum PlatformType { Local = 0, azLocal = 1, azwsTest = 5, azhsStage = 10, azhsProd = 20 };

        internal static class Tags
        {
            internal const string realm_ = "realm_";
            internal const string wifConfig = "wifConfig";
            internal const string wifLocalTestServerName = "wifLocalTestServerName";
        }
        public static class WIFConfigNames
        {
            internal const string azhsProd = "azhsProd";
            internal const string azhsStage = "azhsStage";
            internal const string azwsTest = "azwsTest";
            internal const string local = "local";
            internal const string azLocal = "azLocal";
        }

        public static void RegisterFederatedAuth(EventHandler<Microsoft.IdentityModel.Web.Configuration.ServiceConfigurationCreatedEventArgs> eventHandler)
        {
            FederatedAuthentication.ServiceConfigurationCreated += eventHandler;
        }

        public static string GetWIFConfigName(IDefaultValueProvider valueProvider)
        {
            Nullable<PlatformType> platformType;
            return GetWIFConfigName(valueProvider, out platformType);
        }

        public static string GetWIFConfigName(IDefaultValueProvider valueProvider, out Nullable<PlatformType> platformType)
        {
            platformType = null;
            if (valueProvider == null)
                throw new ArgumentNullException("valueProvider");

            string wifLocalTestServerName = valueProvider.GetStringValueOrNull(Tags.wifLocalTestServerName);
            string wifConfiguredName = valueProvider.GetStringValueOrNull(Tags.wifConfig);
            string wifConfig = null;
            if (!string.IsNullOrEmpty(wifLocalTestServerName) && wifLocalTestServerName.Equals(Environment.MachineName, StringComparison.InvariantCultureIgnoreCase))
            {
                wifConfig = WIFConfigNames.local;
                if (!string.IsNullOrWhiteSpace(wifConfiguredName) && wifConfiguredName.Equals(WIFConfigNames.azLocal))
                {
                    wifConfig = wifConfiguredName;
                    platformType = PlatformType.azLocal;
                }
                else
                {
                    platformType = PlatformType.Local;
                }
            }
            else if (!string.IsNullOrEmpty(wifConfiguredName))
            {
                switch (wifConfiguredName)
                {
                    case WIFConfigNames.azhsProd:
                        wifConfig = wifConfiguredName;
                        platformType = PlatformType.azhsProd;
                        break;
                    case WIFConfigNames.azhsStage:
                        wifConfig = wifConfiguredName;
                        platformType = PlatformType.azhsStage;
                        break;
                    case WIFConfigNames.azwsTest:
                        wifConfig = wifConfiguredName;
                        platformType = PlatformType.azwsTest;
                        break;
                }
            }
            if (!platformType.HasValue)
            {
                wifConfig = WIFConfigNames.azhsProd;
                platformType = PlatformType.azhsProd;
            }
            return wifConfig;
        }

        public static string GetWIFRealm(IDefaultValueProvider valueProvider, Nullable<PlatformType> platformType)
        {
            string wifRealmTag = string.Format("{0}{1}", Tags.realm_, platformType);
            string wifRealm = valueProvider.GetStringValueOrNull(wifRealmTag);
            return wifRealm;
        }
    }
}